package com.jt.tenant.popwindow;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import com.jt.tenant.R;
import com.jt.tenant.activity.MaintainActivity;
import com.jt.tenant.adapter.SortAdapter;
import com.jt.tenant.conn.Get4SShopList;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/28.
 */
public class SortListPopup extends PopupWindow{
    private View rootView; // 总的布局
    private ListView mList;
    private List<Get4SShopList.AreaList> list = new ArrayList<>();
    private SortAdapter mAdapter;
    private OnItemClick onItemClick;
    private Context mcontext;
    public SortListPopup(Context context, List<Get4SShopList.AreaList> mlist) {
        super(context);
        this.setWidth(ViewGroup.LayoutParams.FILL_PARENT);
        this.setHeight(ViewGroup.LayoutParams.FILL_PARENT);
        this.setBackgroundDrawable(new BitmapDrawable());// 这样设置才能点击屏幕外dismiss窗口
//        this.setOutsideTouchable(true);
//        this.setAnimationStyle(com.bigkoo.pickerview.R.style.timepopwindow_anim_style);
        this.list.addAll(mlist);
        this.mcontext = context;
        setFocusable(true);
        LayoutInflater mLayoutInflater = LayoutInflater.from(context);
        rootView = mLayoutInflater.inflate(R.layout.pop_sort_list, null);

        mList = (ListView) rootView.findViewById(R.id.list_pop_sort);

        mAdapter = new SortAdapter(context,list);

        mList.setAdapter(mAdapter);

        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                onItemClick.onItemClick(list.get(position));
                dismiss();

            }
        });


        rootView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (isShowing()) {
                    dismiss();
                }
                return false;
            }
        });

        setContentView(rootView);
    }


    public void show(View view){

        showAtLocation(view, Gravity.BOTTOM, 0, 0);

    }

    public void changeList(List<Get4SShopList.AreaList> mlist){

        list.clear();

        list.addAll(mlist);

        mAdapter.notifyDataSetChanged();

    }

    public interface OnItemClick{

        public void onItemClick(Get4SShopList.AreaList area);

    }

    public void setOnItemClickListener(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

}